package cn.life.business.literature.controller;

import cn.life.common.entity.model.CMessage;
import cn.life.business.literature.entity.po.Literature;
import cn.life.business.literature.entity.vo.LiteratuerVo;
import cn.life.business.literature.service.LiteratureService;
import cn.life.common.entity.model.MyUser;
import cn.life.common.entity.model.MyUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;

/**
 * @Description:    文献操作控制层
 * @author wangzhenyu
 * @Date 2018/7/11
 */
@RestController
@RequestMapping("literature")
public class LiteratureController {

    @Autowired
    private LiteratureService literatureService;

    /**
     * @Description:  文献添加
     * @param   literature
     * @return
     */
    @PostMapping(value = "/addliterature")
    public CMessage addLiterature(@RequestBody Literature literature, @MyUser MyUserInfo myUserInfo){
         getOpr(literature,myUserInfo);
        return literatureService.addLiterature(literature);
    }
    /**
     * @Description:  文献删除
     * @param   literature
     * @return
     */
    @PostMapping(value = "/deleteliterture")
    public  CMessage deleteLiterature(@RequestBody Literature literature){
        return literatureService.deleteLiterature(literature.getLiteratureId());
    }
    /**
     * @Description:  文献修改
     * @param   literature
     * @return
     */
    @PostMapping(value = "/updateliterture")
    public CMessage updateLiterature(@RequestBody Literature literature){
        return literatureService.updateLiterature(literature);
    }
    /**
     * @Description:  查询文献
     * @param   literatuerVo
     * @return
     */
    @PostMapping(value = "/getalliteratureinfo")
    public CMessage getAllLiteratureInfo(@RequestBody LiteratuerVo literatuerVo) throws ParseException {
        return literatureService.getAllLiteratureInfo(literatuerVo);
    }

    /**
     * @Description:  查询单条（详情页面）文献
     * @param   literatuerVo
     * @return
     */
    @PostMapping(value = "/getliterature")
    public CMessage getLiterature(@RequestBody LiteratuerVo literatuerVo){
        return literatureService.getLiterature(literatuerVo);
    }

    /**
     * @Description:  自动添加项
     * @param   literature,myUserInfo
     * @return
     */
    public Literature getOpr(Literature literature, MyUserInfo myUserInfo){
     literature.setOprUserId(myUserInfo.getUserId());
     literature.setOprUserName(myUserInfo.getUserName());
     literature.setOprDate(new Date());
      return literature;
    }
}
